<template>
  <zm-view></zm-view>
</template>

<script>
export default {
  data() {
    return {
      path: "",
      appid: "",
      title: "",
      jumped: false
    }
  },
  onShow() {
    if (this.jumped) {
      uni.navigateBack()
    }
  },
  onLoad(options) {
    this.path = options.path || options.url
    this.appid = options.appid
    this.title = options.title
    if (this.title) uni.setNavigationBarTitle({ title: this.title })
    this.handleJump()
  },
  methods: {
    handleJump() {
      if (this.appid && this.path) {
        uni.$dialog({
          title: "跳转提示",
          content: "即将离开当前小程序，是否同意访问其他小程序?",
          cancelButtonText: "取消",
          confirmButtonText: "同意",
          showCancelButton: true,
          onCancel: () => {
            uni.navigateBack()
          },
          onConfirm: () => {
            uni.navigateToMiniProgram({
              appId: this.appid,
              path: decodeURIComponent(this.path),
              success: () => {
                this.jumped = true
              },
              fail: (e) => {
                const pattern = /cancel/
                // if (pattern.test(e.errMsg)) {
                //   uni.navigateBack()
                // } else {
                uni.$dialog({
                  title: "提示",
                  content: JSON.stringify(e),
                  onConfirm: () => {
                    uni.navigateBack()
                  }
                })
                // }
              }
            })
          }
        })
      } else {
        uni.$dialog({
          title: "提示",
          content: "小程序ID或跳转地址不正确，请检查",
          onConfirm: () => {
            uni.navigateBack()
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
